package com.software.loginregister.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.FoodItem;
import com.software.loginregister.entity.vo.FoodRecognitionDTO;

import java.util.ArrayList;
import java.util.List;

public class ManualFoodAdapter extends RecyclerView.Adapter<ManualFoodAdapter.FoodViewHolder> {
    private List<FoodItem> foodList;
    private Context context;

    public ManualFoodAdapter(Context context) {
        this.context = context;
        this.foodList = new ArrayList<>();
    }

    @NonNull
    @Override
    public FoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_food, parent, false);
        return new FoodViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FoodViewHolder holder, int position) {
        FoodItem item = foodList.get(position);
        holder.tvFoodName.setText(item.getName());
        holder.tvQuantity.setText(String.format("%d %s", item.getQuantity(), item.getUnit()));

        holder.btnEdit.setOnClickListener(v -> showEditDialog(position));
        holder.btnDelete.setOnClickListener(v -> removeItem(position));
    }

    @Override
    public int getItemCount() {
        return foodList.size();
    }

    public void addItem(FoodItem item) {
        foodList.add(item);
        notifyItemInserted(foodList.size() - 1);
    }

    private void showEditDialog(int position) {
        FoodItem item = foodList.get(position);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_edit_food2, null);

        EditText etName = dialogView.findViewById(R.id.etName);
        EditText etQuantity = dialogView.findViewById(R.id.etQuantity);
        EditText etUnit = dialogView.findViewById(R.id.etUnit);
        EditText etStorage = dialogView.findViewById(R.id.etStorage);
        EditText etDays = dialogView.findViewById(R.id.etDays);

        etName.setText(item.getName());
        etQuantity.setText(String.valueOf(item.getQuantity()));
        etUnit.setText(item.getUnit());
        etStorage.setText(item.getStorage());
        etDays.setText(String.valueOf(item.getDays()));

        builder.setView(dialogView)
                .setTitle("修改食材")
                .setPositiveButton("确定", (dialog, which) -> {
                    String newName = etName.getText().toString();
                    int newQuantity = Integer.parseInt(etQuantity.getText().toString());
                    String newUnit = etUnit.getText().toString();
                    String newStorage = etStorage.getText().toString();
                    int newDays = Integer.parseInt(etDays.getText().toString());

                    item.setName(newName);
                    item.setQuantity(newQuantity);
                    item.setUnit(newUnit);
                    item.setStorage(newStorage);
                    item.setDays(newDays);
                    notifyItemChanged(position);
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void removeItem(int position) {
        foodList.remove(position);
        notifyItemRemoved(position);
    }

    public List<FoodRecognitionDTO> getFoodRecognitionList() {
        List<FoodRecognitionDTO> dtoList = new ArrayList<>();
        for (FoodItem item : foodList) {
            FoodRecognitionDTO dto = new FoodRecognitionDTO();
            dto.setName(item.getName());
            dto.setQuantity(item.getQuantity());
            dto.setUnit(item.getUnit());
            dto.setStorage(item.getStorage());
            dto.setDays(item.getDays());
            dtoList.add(dto);
        }
        return dtoList;
    }

    static class FoodViewHolder extends RecyclerView.ViewHolder {
        TextView tvFoodName;
        TextView tvQuantity;
        Button btnEdit;
        ImageButton btnDelete;

        public FoodViewHolder(@NonNull View itemView) {
            super(itemView);
            tvFoodName = itemView.findViewById(R.id.tvFoodName);
            tvQuantity = itemView.findViewById(R.id.tvQuantity);
            btnEdit = itemView.findViewById(R.id.btnEdit);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }
}

